package com.grandbroad.eim.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 *
 * 时间处理工具
 * Created by dsad on 2018/3/26.
 */
public class TimeUtils {
    private static final String TAG = "TimeUtil";
    public volatile static TimeUtils instance;

    public static TimeUtils getInstance() {
        if (instance == null) {
            synchronized (TimeUtils.class) {
                if (instance == null) {
                    instance = new TimeUtils();
                }
            }
        }
        return instance;
    }

    /**
     * 当前时间与传来的时间对比
     * 之前
     *
     * @param time
     * @return
     */
    public static String computePastTime(String time) {
        // Log.v(TAG, "computePastTime: " + time);
        String result = "刚刚";
        //2017-02-13T01:20:13.035+08:00
        //time = time.replace("T", " ");
        //time = time.substring(0, 16);
        // Log.v(TAG, "computePastTime time: " + time);
        SimpleDateFormat simpleDateFormat =
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.SIMPLIFIED_CHINESE);
        try {
            Date t = simpleDateFormat.parse(time);
            Date now = new Date(System.currentTimeMillis());
            long diff = (now.getTime() - t.getTime()) / 1000;
            if (diff < 60) {
                result = "刚刚";
            } else if ((diff /= 60) < 60) {
                result = diff + "分钟前";
            } else if ((diff /= 60) < 24) {
                result = diff + "小时前";
            } else if ((diff /= 24) < 30) {
                result = diff + "天前";
            } else if ((diff /= 30) < 12) {
                result = diff + "月前";
            } else {
                diff /= 12;
                result = diff + "年前";
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        // Log.v(TAG, "computePastTime result: " + result);
        return result;
    }

    /**
     * 之后
     * 确保传来的时间比现在的时间长
     *
     * @param time
     * @return
     */
    public static String computePastTimeAfter(String time) {
        // Log.v(TAG, "computePastTime: " + time);
        String result = "未知";
        //2017-02-13T01:20:13.035+08:00
        //time = time.replace("T", " ");
        //time = time.substring(0, 16);
        // Log.v(TAG, "computePastTime time: " + time);
        SimpleDateFormat simpleDateFormat =
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.SIMPLIFIED_CHINESE);
        try {
            Date t = simpleDateFormat.parse(time);
            Date now = new Date(System.currentTimeMillis());
            /*
            long diff = (now.getTime() - t.getTime()) / 1000;
            if (diff < 60) {
                result = "刚刚";
            } else if ((diff /= 60) < 60) {
                result = diff + "分钟前";
            } else if ((diff /= 60) < 24) {
                result = diff + "小时前";
            } else if ((diff /= 24) < 30) {
                result = diff + "天前";
            } else if ((diff /= 30) < 12) {
                result = diff + "月前";
            } else {
                diff /= 12;
                result = diff + "年前";
            }
            */
            long diff = (t.getTime() - now.getTime()) / 1000;
            if (diff < 60) {
                result = "1分钟";
            } else if ((diff /= 60) < 60) {
                result = diff + "分钟";
            } else if ((diff /= 60) < 24) {
                result = diff + "小时";
            } else if ((diff /= 24) < 30) {
                result = diff + "天";
            } else if ((diff /= 30) < 12) {
                result = diff + "月";
            } else {
                diff /= 12;
                result = diff + "年";
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        // Log.v(TAG, "computePastTime result: " + result);
        return result;
    }

    /***
     * 格式化时间
     * @param time
     * @return
     */
    public static String parseTime(String time) {
        // Log.v(TAG, "formatTime: " + time);
        //2017-02-13T01:20:13.035+08:00
        time = time.replace("T", " ");
        time = time.substring(0, 16);
        // Log.v(TAG, "formatTime result: " + time);
        return time;
    }

    public String formatTime(String time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.SIMPLIFIED_CHINESE);
        return simpleDateFormat.format(new Date(time));
    }

    /**
     * 转化gt时间
     *(对两种时间类型进行处理)
     * @param time
     * @return
     */
    public static String formatGTtime(String time) {
        //换成里中国近的时区
        SimpleDateFormat sf1 = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy", Locale.ENGLISH);
        try {
            Date date = sf1.parse(time);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd", Locale.SIMPLIFIED_CHINESE);
            return simpleDateFormat.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
            //这里是正常时间
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.SIMPLIFIED_CHINESE);
            try {
                Date date = simpleDateFormat.parse(time);
                SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("MM-dd", Locale.SIMPLIFIED_CHINESE);
                return simpleDateFormat1.format(date);
            } catch (ParseException e1) {
                e1.printStackTrace();
            }
            return "时间错误";
        }
    }

    public static String formatGT(String time)
    {
        String[] aStrings = time.split(" ");
        if (aStrings[1].equals("Jan"))
        {
            aStrings[1] = "01";
        }
        if (aStrings[1].equals("Feb"))
        {
            aStrings[1] = "02";
        }
        if (aStrings[1].equals("Mar"))
        {
            aStrings[1] = "03";
        }
        if (aStrings[1].equals("Apr")) {
            aStrings[1] = "04";
        }
        if (aStrings[1].equals("May")) {
            aStrings[1] = "05";
        }
        if (aStrings[1].equals("Jun")) {
            aStrings[1] = "06";
        }
        if (aStrings[1].equals("Jul")) {
            aStrings[1] = "07";
        }
        if (aStrings[1].equals("Aug")) {
            aStrings[1] = "08";
        }
        if (aStrings[1].equals("Sep")) {
            aStrings[1] = "09";
        }
        if (aStrings[1].equals("Oct")) {
            aStrings[1] = "10";
        }
        if (aStrings[1].equals("Nov")) {
            aStrings[1] = "11";
        }
        if (aStrings[1].equals("Dec")) {
            aStrings[1] = "12";
        }
        return aStrings[5] + "-" + aStrings[1] + "-" + aStrings[2] + " " + aStrings[3];
    }



}